package com.morgan.maxfinance;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class NewIndexActivity extends Activity {

	LinearLayout llytHeader;
	
	Button btnNewApplication;
	Button btnReLoan;
	Button btnFileUpload;
	Button btnFacebook;
	Button btnTwitter;

	public static final int TYPE_NEW_APP = 0;
	public static final int TYPE_RE_LOAN = 1;

	public static int nType = TYPE_NEW_APP;

	TextView txtSmallBusiness;
	TextView txtGetToToday;
	TextView txtIndexDescription;

	TextView txtDecisionIn5Mins;
	TextView txtSameDayFunding;
	TextView txtBadCreditConsidered;
	TextView txtNewBusinessOk;

	ImageView btnDecisionIn5Mins;
	ImageView btnSameDayFunding;
	ImageView btnBadCreditConsidered;
	ImageView btnNewBusinessOk;

	TextView txtHelp;
	TextView txtContactUs;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_index);

		initWidget();
		initValue();
		initEvent();

		if (nType == TYPE_NEW_APP) {
			// btnNewApplication.setBackgroundColor(0xff1488c7);
			// btnReLoan.setBackgroundColor(0xffbbbbbb);
			// btnReLoan.setEnabled(false);
			btnNewApplication.setVisibility(View.VISIBLE);
			btnReLoan.setVisibility(View.GONE);
		}

		if (nType == TYPE_RE_LOAN) {
			// btnNewApplication.setBackgroundColor(0xffbbbbbb);
			// btnReLoan.setBackgroundColor(0xff1488c7);
			// btnNewApplication.setEnabled(false);
			btnNewApplication.setVisibility(View.GONE);
			btnReLoan.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

	private void initWidget() {
		
		llytHeader = (LinearLayout) findViewById(R.id.header_linearLayout);

		btnNewApplication = (Button) findViewById(R.id.new_application_button);
		btnReLoan = (Button) findViewById(R.id.re_loan_button);
		btnFileUpload = (Button) findViewById(R.id.file_upload_button);

		txtSmallBusiness = (TextView) findViewById(R.id.small_business_loans_textView);
		txtGetToToday = (TextView) findViewById(R.id.your_contract_detail_caption_textView);
		txtIndexDescription = (TextView) findViewById(R.id.your_contract_detail_content_textView);

		txtDecisionIn5Mins = (TextView) findViewById(R.id.decision_in_5mins_textView);
		txtSameDayFunding = (TextView) findViewById(R.id.same_day_funding_textView);
		txtBadCreditConsidered = (TextView) findViewById(R.id.bad_credit_considered_textView);
		txtNewBusinessOk = (TextView) findViewById(R.id.new_business_ok_textView);

		btnDecisionIn5Mins = (ImageView) findViewById(R.id.inf_decision_imageView);
		btnSameDayFunding = (ImageView) findViewById(R.id.inf_same_day_imageView);
		btnBadCreditConsidered = (ImageView) findViewById(R.id.inf_bad_credit_imageView);
		btnNewBusinessOk = (ImageView) findViewById(R.id.inf_new_business_imageView);

		txtHelp = (TextView) findViewById(R.id.help_textView);
		txtContactUs = (TextView) findViewById(R.id.contact_us_textView);
		
		btnFacebook 			= (Button) findViewById(R.id.facebook_button);
		btnTwitter 				= (Button) findViewById(R.id.twitter_button);
	}

	private void initValue() {

		Typeface tfOswaldBold = Typeface.createFromAsset(
				getApplicationContext().getAssets(), "font/Oswald-Bold.ttf");
		Typeface tfOswaldLight = Typeface.createFromAsset(
				getApplicationContext().getAssets(), "font/Oswald-Light.ttf");
		Typeface tfOswaldReqular = Typeface.createFromAsset(
				getApplicationContext().getAssets(), "font/Oswald-Regular.ttf");

		txtSmallBusiness.setTypeface(tfOswaldBold);
		txtGetToToday.setTypeface(tfOswaldBold);
		txtIndexDescription.setTypeface(tfOswaldReqular);

		txtDecisionIn5Mins.setTypeface(tfOswaldReqular);
		txtSameDayFunding.setTypeface(tfOswaldReqular);
		txtBadCreditConsidered.setTypeface(tfOswaldReqular);
		txtNewBusinessOk.setTypeface(tfOswaldReqular);
	}

	private void initEvent() {
		
		llytHeader.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maxfinance.com.au"));
				startActivity(browserIntent);
			}
        });
		
		btnNewApplication.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				NewIndexActivity.this.startActivity(new Intent(NewIndexActivity.this, HomeActivity.class));
			}
        });
		
		btnReLoan.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
//				NewIndexActivity.this.startActivity(new Intent(NewIndexActivity.this, CompleteActivity.class));
				NewIndexActivity.this.startActivity(new Intent(NewIndexActivity.this, HomeActivity.class));
				
			}
        });
		
		btnDecisionIn5Mins.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGeneralAlert("Information", "Max Funding has developed a smart technology to give you instant pre-approval results after submitting your application form. " +
						"Pre-Approval provides you with an indication of your eligibility so you can have a peace of mind.");
			}
        });
		
		btnSameDayFunding.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGeneralAlert("Information", "Your loan is transferred straight into your bank account once your loan settles during business hours. " +
						"Most people have it available instantly, but sometimes it may take longer depending on your bank or if you applied after 3pm on a business day " +
						"or out-of-office hours.");
			}
        });
		
		btnBadCreditConsidered.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGeneralAlert("Information", "We believe in giving second chances, even if you have a bad credit history we will attempt to assist you.");
			}
        });
		
		btnNewBusinessOk.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGeneralAlert("Information", "We love to help Aussies starting businesses.<br /> We understand it is quite difficult to get a loan for start-up businesses, " +
						"hence we have taken all the barriers and made our processes are new business friendly:<br> - No track record required\n" +
						"- No financial required\n" +
						"- No guarantor required\n" +
						"- No business plan required\n" +
						"(or though a simple business description would help)");
			}
        });
		
		txtHelp.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				final AlertDialog.Builder alert = new AlertDialog.Builder(NewIndexActivity.this);
			    final EditText input = new EditText(NewIndexActivity.this);
			    input.setHint("e.g. form won't submit, won't go next page, Other form errors");
			    alert.setTitle("What issues are you experiencing?");
			    alert.setView(input);
			    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			        public void onClick(DialogInterface dialog, int whichButton) {
//			            dialog.cancel();
			        }
			    });
			    alert.setPositiveButton("Talk to Us", new DialogInterface.OnClickListener() {
			        public void onClick(DialogInterface dialog, int whichButton) {
			        	/*
			        	 Intent email = new Intent(Intent.ACTION_SEND_MULTIPLE);
			        	 email.putExtra(Intent.EXTRA_EMAIL, new String[] {"victor@morganfinance.com.au"});
			             email.putExtra(Intent.EXTRA_SUBJECT, "Mobile App technical error report");
			             email.putExtra(Intent.EXTRA_TEXT, input.getText().toString());
			        	 email.setType("text/plain");

			             startActivity(Intent.createChooser(email, "E-mail"));
			             */
			        	String strMessage = input.getText().toString();
	 					new MyAsyncTask(NewIndexActivity.this, MyAsyncTask.ACTION_CONTACT_US).execute(MyAsyncTask.ACTION_HELP, strMessage);
			        }
			    });
			    
			    alert.show();
			}
        });
		
		txtContactUs.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				NewIndexActivity.this.startActivity(new Intent(NewIndexActivity.this, NewContactUsActivity.class));
			}
        });
		
		btnFileUpload.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				NewIndexActivity.this.startActivity(new Intent(NewIndexActivity.this, FileUploadActivity.class));
			}
        });
		
		btnFacebook.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/maxfin"));
				startActivity(browserIntent);
			}
        });
		
		btnTwitter.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/Max_Finance"));
				startActivity(browserIntent);
			}
        });
	}

	public void showGeneralAlert(String title, String message) {
		new AlertDialog.Builder(NewIndexActivity.this).setTitle(title)
				.setMessage(message)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// continue with delete
					}
				}).show();
	}
}
